Passed
Push — master ( 1a6db4...d22713 )
by Muhammad Dyas
01:40
created

TaskHandler.getStateFromMessageId   B

Complexity

Conditions 6

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 14
rs 8.6666
c 0
b 0
f 0
cc 6
1
import {PollState, TaskEvent} from '../helpers/interfaces';
2
import {callMessageApi} from '../helpers/api';
3
import {getStateFromCardName} from '../helpers/state';
4
import PollCard from '../cards/PollCard';
5
6
export default class TaskHandler {
7
  event: TaskEvent;
8
9
  public constructor(event: TaskEvent) {
10
    this.event = event;
11
  }
12
13
  async process(): Promise<void> {
14
    this.event.state = await this.getStateFromMessageId();
15
16
    switch (this.event.action) {
17
      case 'close_poll':
18
        await this.handleClosePollAction();
19
        break;
20
      case 'remind_all':
21
        await this.handleRemindAllAction();
22
        break;
23
      default:
24
        console.log('Unknown task');
25
    }
26
  }
27
28
  private async handleClosePollAction(): Promise<void> {
29
    if (this.event.state!.closedBy) {
30
      console.log('The poll is already closed by', this.event.state!.closedBy);
31
      return;
32
    }
33
34
    if (!this.event.state!.closedTime || this.event.state!.closedTime > Date.now()) {
35
      this.event.state!.closedTime = Date.now();
36
    }
37
38
    this.event.state!.closedBy = 'scheduled auto-close';
39
    const apiResponse = await this.updatePollMessage(this.event.state!);
40
41
    if (apiResponse?.status !== 200) {
42
      throw new Error('Error when closing message');
43
    }
44
  }
45
46
  private async handleRemindAllAction(): Promise<void> {
47
    if (this.event.state!.closedTime && this.event.state!.closedTime > Date.now()) {
48
      await this.remindAll();
49
    }
50
  }
51
52
  async getStateFromMessageId(): Promise<PollState> {
53
    const request = {
54
      name: this.event.id,
55
    };
56
    const apiResponse = await callMessageApi('get', request);
57
    const currentState = getStateFromCardName(apiResponse.data.cardsV2?.[0].card ?? {});
58
    if (!currentState) {
59
      console.log(apiResponse ? JSON.stringify(apiResponse) : 'empty response:' + this.event.id);
60
      throw new Error('State not found');
61
    }
62
    this.event.space = apiResponse.data.space;
63
    this.event.thread = apiResponse.data.thread;
64
    return JSON.parse(currentState) as PollState;
65
  }
66
67
  async updatePollMessage(currentState: PollState) {
68
    const localeTimezone = {locale: 'en', offset: 0, id: 'UTC'};
69
    const cardMessage = new PollCard(currentState, localeTimezone).createMessage();
70
    const request = {
71
      name: this.event.id,
72
      requestBody: cardMessage,
73
      updateMask: 'cardsV2',
74
    };
75
    return await callMessageApi('update', request);
76
  }
77
78
  async remindAll() {
79
    const text = `<users/all>, The poll with the topic *${this.event.state!.topic}*  is reaching its finale. Please wrap up your voting now.`;
80
81
    const request = {
82
      name: this.event.id,
83
      parent: this.event.space!.name,
84
      messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
85
      requestBody: {
86
        text, thread: this.event.thread,
87
      },
88
    };
89
    return await callMessageApi('create', request);
90
  }
91
}
92